import { AES, SHA256, enc, mode, pad } from 'crypto-js';

const key = enc.Utf8.parse('DSqBPJdfMXdsHBdS');

/** AES加密 */
export function Encrypt(str: number | string): string {
  if (!str) {
    return String(str);
  }
  const encryptedData = AES.encrypt(String(str), key, { mode: mode.ECB, padding: pad.Pkcs7 });
  return encryptedData.toString();
}

/** AES解密 */
export function UnEncrypt(str: number | string): string {
  if (!str) {
    return String(str);
  }
  const encryptedHexStr = enc.Base64.parse(String(str));
  const encryptedBase64Str = enc.Base64.stringify(encryptedHexStr);
  const decryptedData = AES.decrypt(encryptedBase64Str, key, {
    mode: mode.ECB,
    padding: pad.Pkcs7,
  });
  return decryptedData.toString(enc.Utf8);
}

/** 得到SHA256 */
export function getSHA256(str: string) {
  return SHA256(str).toString();
}
